#!/bin/bash
# Description : Add Brand New Project Environment OR New mini-service
# Author      : liang.zhou01
# modify date : 2017.03.25 by liang.zhou01
# Modify      : 2017.04.22 by hongzheng.liu

if [[ $1 == "" ]];then
    echo "Usage: $0 ***.msfinance.lo [port]"
    exit 1
fi

DOMAIN=$1
# get first part of $DOMAIN
PRE_DOMAIN=${DOMAIN%.*}
# nginx config template
NGINX_CONF_TMPL=$(readlink -f `dirname $0`)/xxx.msfinance.cn

function BASE_WORKS () {
    mkdir -p /home/finance/App/
    mkdir -p /home/finance/Shell/
    mkdir -p /home/finance/Logs/
    mkdir -p /home/finance/Data/
    mkdir -p /home/finance/Logs/nginx/
}
#First time,creat base directory
BASE_WORKS

######################## Judging A Server is New or Old ################################
if [[  "$#" == 2 && "$2" =~ ^[0-9]+$ ]]; then
    HTTPPORT=$2
else
    if [[ `ls /home/finance/Domains/|wc -l` == 0 && `ls -l /home/finance/App/*/release/|wc -l` == 0 ]]
    then
        DOWNPORT="9001"
        HTTPPORT="8001"
    else
        if [[ -d "/home/finance/Domains" && `ls /home/finance/Domains/|wc -l` > 0 ]]; then
            cat /home/finance/Domains/*/*/conf/server.xml |grep "HTTP" |awk '{print $2}'|awk -F "=" '{print $2}' |sed 's/\"//g' > /tmp/ports.txt 2>&1
        fi
        
        if [[ -d "/home/finance/App/" && `ls /home/finance/App/|wc -l` > 0 ]]; then
            ls -l /home/finance/App/*/servers/ | grep "^d" | awk '{print $9}' >> /tmp/ports.txt
        fi
        HTTP_MAXPORT=`cat /tmp/ports.txt |awk 'BEGIN{x=$1;} {for(i=1;i<=NF;i++)if($i>x)x=$i;} END{print x}'`
        HTTPPORT=`awk 'BEGIN{nu=('${HTTP_MAXPORT}'+1);print nu}'`
    fi
fi

## 新建服务
function APP_NEW(){
    mkdir -p /home/finance/App/${DOMAIN}
    mkdir -p /home/finance/App/${DOMAIN}/servers
    mkdir -p /home/finance/App/${DOMAIN}/release	
    mkdir -p /home/finance/Logs/${DOMAIN}
    mkdir -p /home/finance/Data/${DOMAIN}

    mkdir -p /home/finance/Logs/nginx/${DOMAIN}
    
    APP_SETENV=/home/finance/App/${DOMAIN}/setenv.sh

    echo -e "#!/bin/bash\n\n#MY_OPTS=\"-Djava.library.path=/usr/local/lib -server -Xms1024m -Xmx1024m -XX:MaxPermSize=256m -Djava.awt.headless=true -Dsun.net.client.defaultConnectTimeout=60000 -Dsun.net.client.defaultReadTimeout=60000 -Djmagick.systemclassloader=no -Dnetworkaddress.cache.ttl=300 -Dsun.net.inetaddr.ttl=300\"\n\nif [[ \"\$MY_OPTS\" != \"\" ]]; then\nAPP_OPTS=\$MY_OPTS\nelse\nAPP_OPTS=\$JAVA_OPTS\nfi \n" > ${APP_SETENV}
	
    eval "chmod +x ${APP_SETENV}"
}

## 增加服务实例
function APP_ADD(){
    mkdir -p /home/finance/App/${DOMAIN}/servers/${HTTPPORT}

    chown -R finance.finance /home/finance/App
    chown -R finance.finance /home/finance/Logs
    chown -R finance.finance /opt/nginx/conf/domains/
    #添加开机启动等脚本确定再说
    #echo "su - finance -c /home/finance/Shell/run.sh start ${PRE_DOMAIN} ${HTTPPORT}" >>/etc/rc.local 
}

################### SET NGINX SERVER ################################ 
function NGINX_NEW (){
    PROXY_NAME=${PRE_DOMAIN}
    NEW_NGINX_FILE="/opt/nginx/conf/domains/${DOMAIN}"
    cp ${NGINX_CONF_TMPL} ${NEW_NGINX_FILE}
    sed -i "s#xxx.msfinance.cn#${DOMAIN}#g" ${NEW_NGINX_FILE}
    sed -i "s#tomcat_xxx#tomcat_${PROXY_NAME}#g" ${NEW_NGINX_FILE}
    sed -i "s#8001#${HTTPPORT}#g" ${NEW_NGINX_FILE}
    #add msfinance.cn host head
    sed -i "/server_name/s#;# ${PRE_DOMAIN}\.com;#g" ${NEW_NGINX_FILE}
    echo "Nginx Config Complete...."
}

function NGINX_ADD () {
    NGINX_TMP_FILE="/opt/nginx/conf/domains/${DOMAIN}_tmp"
    NGINX_FILE="/opt/nginx/conf/domains/${DOMAIN}"
    LINE_NUM=`cat -n /opt/nginx/conf/domains/${DOMAIN} |grep "server 127"|tail -n 1|awk '{print $1}'`
    sed "${LINE_NUM}a\\\t\tserver\ 127.0.0.1\:${HTTPPORT}\ \ weight\=10\ max_fails\=2\ fail_timeout\=30s\;" ${NGINX_FILE}>${NGINX_TMP_FILE}
    mv -f ${NGINX_TMP_FILE} ${NGINX_FILE} 
    echo "Nginx Config Complete...."
}

################## FOR REAL ###############################
if [ -d /home/finance/App/${DOMAIN}/servers ];then
    echo -n "Application $1 exists, Would you like to add a instance?(yes/no)"
    read YES
    case $YES in
        y|yes|Y|YES)
            NGINX_ADD
            APP_ADD
            ;;
        n|no|N|NO)
            exit
            ;;
    esac
else
    NGINX_NEW
    APP_NEW
    APP_ADD
fi
